package com.jiuyue.excel_time.component;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.excel.util.ListUtils;
import com.jiuyue.excel_time.entity.CustomerExcel;
import com.jiuyue.excel_time.service.CustomerExcelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @description: excel读取监听器
 * @author: Shihaiqing
 * @date: 2023-12-23 14:47
 * @version: 1.0
 **/
@Slf4j
@Component
public class CustomerExcelDataListener implements ReadListener<CustomerExcel> {
    /**
     * 每隔5条存储数据库，实际使用中可以100条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 100;

    private static  int BATCH_FINISH=0;

    /**
     * 缓存的数据
     */
    private List<CustomerExcel> cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
    /**
     * 假设这个是一个DAO，当然有业务逻辑这个也可以是一个service。当然如果不用存储这个对象没用。
     */
    private CustomerExcelService customerExcelService;

    /**
     * 每次创建Listener的时候需要把spring管理的类传进来
     *
     * @param customerExcelService
     */
    public CustomerExcelDataListener(CustomerExcelService customerExcelService) {
        this.customerExcelService = customerExcelService;
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param customerExcel    one row value. Is is same as {@link AnalysisContext#readRowHolder()}
     * @param context
     */
    @Override
    public void invoke(CustomerExcel customerExcel, AnalysisContext context) {
        Integer rowNumber = context.readSheetHolder().getApproximateTotalRowNumber();
        cachedDataList.add(customerExcel);
        // 达到BATCH_COUNT了，需要去存储一次数据库，防止数据几万条数据在内存，容易OOM
        if (cachedDataList.size() >= BATCH_COUNT) {
            this.customerExcelService.save(cachedDataList);
            customerExcelService.save(cachedDataList);
            BATCH_FINISH ++;
            // 存储完成清理 list
            cachedDataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT);
            int finishedCustomers =BATCH_FINISH*BATCH_COUNT;
            log.info("======已经完成进度{}====",getFinishCustomer(finishedCustomers,rowNumber));
        }
    }


    public  String getFinishCustomer(int finish,int total){
        // 将被除数转换为BigDecimal类型
        BigDecimal dividendBigDecimal = new BigDecimal(finish);
        BigDecimal divisorBigDecimal = new BigDecimal(total);
        // 使用BigDecimal进行除法计算，并指定保留两位小数和舍入方式
        BigDecimal resultBigDecimal = dividendBigDecimal.divide(divisorBigDecimal, 2, RoundingMode.HALF_UP);// 输出结果的字符串形式
        String s = resultBigDecimal.multiply(BigDecimal.valueOf(100)).toString();
        return s+"%";
    }



    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("======已经完成进度{}====","100%");
    }

    /**
     * 加上存储数据库
     */
    private void saveData() {
        log.info("{}条数据，开始存储数据库！", cachedDataList.size());
        //customerExcelService.save(cachedDataList);
        log.info("存储数据库成功！");
    }

}
